package gui.input.manager;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.PojoObservables;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;

import data.model.ProjectCharacteristics;

public class ProjectCharacteristicsDialog extends Dialog {
	private DataBindingContext m_bindingContext;
	private final ProjectCharacteristics projectCharacteristics;
	private Spinner spinnerSuccessfulTests;
	private Spinner spinnerCodeCoverage;
	private Spinner spinnerNumberOfTestcases;
	private Spinner spinnerNumberOfComponents;
	private Spinner spinnerLinesOfCode;
	private Spinner spinnerNumberOfBugs;
	private Spinner spinnerNumberOfTasks;
	private final Composite f_composite;

	/**
	 * Create the dialog.
	 * 
	 * @param composite
	 * 
	 * @param parent
	 * @param style
	 */
	public ProjectCharacteristicsDialog(Composite composite, Shell parentShell,
			ProjectCharacteristics projectCharacteristics) {
		super(parentShell);
		f_composite = composite;
		this.projectCharacteristics = projectCharacteristics;
	}

	/**
	 * Create contents of the dialog.
	 * 
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		GridLayout gridLayout = (GridLayout) container.getLayout();
		gridLayout.numColumns = 2;

		Label lblNumberOfTasks = new Label(container, SWT.NONE);
		lblNumberOfTasks.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER,
				false, false, 1, 1));
		lblNumberOfTasks.setText("Number of Tasks:");

		spinnerNumberOfTasks = new Spinner(container, SWT.BORDER);
		spinnerNumberOfTasks.setMaximum(Integer.MAX_VALUE);
		spinnerNumberOfTasks.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
				false, false, 1, 1));

		Label lblNumberOfBugs = new Label(container, SWT.NONE);
		lblNumberOfBugs.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER,
				false, false, 1, 1));
		lblNumberOfBugs.setText("Number of Bugs:");

		spinnerNumberOfBugs = new Spinner(container, SWT.BORDER);
		spinnerNumberOfBugs.setMaximum(Integer.MAX_VALUE);
		spinnerNumberOfBugs.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
				false, false, 1, 1));

		Label lblLinesOfCode = new Label(container, SWT.NONE);
		lblLinesOfCode.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblLinesOfCode.setText("Lines of Code:");

		spinnerLinesOfCode = new Spinner(container, SWT.BORDER);
		spinnerLinesOfCode.setIncrement(100);
		spinnerLinesOfCode.setPageIncrement(1000);
		spinnerLinesOfCode.setMaximum(Integer.MAX_VALUE);
		GridData gd_spinnerLinesOfCode = new GridData(SWT.FILL, SWT.CENTER,
				false, false, 1, 1);
		gd_spinnerLinesOfCode.widthHint = 107;
		spinnerLinesOfCode.setLayoutData(gd_spinnerLinesOfCode);

		Label lblNumberOfComponents = new Label(container, SWT.NONE);
		lblNumberOfComponents.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER,
				false, false, 1, 1));
		lblNumberOfComponents.setText("Number of Components:");

		spinnerNumberOfComponents = new Spinner(container, SWT.BORDER);
		spinnerNumberOfComponents.setMaximum(Integer.MAX_VALUE);
		spinnerNumberOfComponents.setLayoutData(new GridData(SWT.FILL,
				SWT.CENTER, false, false, 1, 1));

		Label lblNumberOfTestcases = new Label(container, SWT.NONE);
		lblNumberOfTestcases.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER,
				false, false, 1, 1));
		lblNumberOfTestcases.setText("Number of Testcases:");

		spinnerNumberOfTestcases = new Spinner(container, SWT.BORDER);
		spinnerNumberOfTestcases.setMaximum(Integer.MAX_VALUE);
		spinnerNumberOfTestcases.setLayoutData(new GridData(SWT.FILL,
				SWT.CENTER, false, false, 1, 1));

		Label lblSuccessfulTests = new Label(container, SWT.NONE);
		lblSuccessfulTests.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER,
				false, false, 1, 1));
		lblSuccessfulTests.setText("Successful Tests [in percent]:");

		spinnerSuccessfulTests = new Spinner(container, SWT.BORDER);
		spinnerSuccessfulTests.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
				false, false, 1, 1));

		Label lblCodeCoverage = new Label(container, SWT.NONE);
		lblCodeCoverage.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER,
				false, false, 1, 1));
		lblCodeCoverage.setText("Code Coverage [in percent]:");

		spinnerCodeCoverage = new Spinner(container, SWT.BORDER);
		spinnerCodeCoverage.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
				false, false, 1, 1));

		return container;

	}

	/**
	 * Create contents of the button bar.
	 * 
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		Button button = createButton(parent, IDialogConstants.OK_ID,
				IDialogConstants.OK_LABEL, true);
		button.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				f_composite.layout(true, true);
			}
		});
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {

			}
		});
		button.setText("&OK");
		m_bindingContext = initDataBindings();
	}

	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();
		//
		IObservableValue spinnerNumberOfTasksObserveSelectionObserveWidget = SWTObservables
				.observeSelection(spinnerNumberOfTasks);
		IObservableValue projectCharacteristicsNumberOfTasksObserveValue = PojoObservables
				.observeValue(projectCharacteristics, "numberOfTasks");
		bindingContext.bindValue(
				spinnerNumberOfTasksObserveSelectionObserveWidget,
				projectCharacteristicsNumberOfTasksObserveValue, null, null);
		//
		IObservableValue spinnerNumberOfBugsObserveSelectionObserveWidget = SWTObservables
				.observeSelection(spinnerNumberOfBugs);
		IObservableValue projectCharacteristicsNumberOfBugsObserveValue = PojoObservables
				.observeValue(projectCharacteristics, "numberOfBugs");
		bindingContext.bindValue(
				spinnerNumberOfBugsObserveSelectionObserveWidget,
				projectCharacteristicsNumberOfBugsObserveValue, null, null);
		//
		IObservableValue spinnerLinesOfCodeObserveSelectionObserveWidget = SWTObservables
				.observeSelection(spinnerLinesOfCode);
		IObservableValue projectCharacteristicsLinesOfCodeObserveValue = PojoObservables
				.observeValue(projectCharacteristics, "linesOfCode");
		bindingContext.bindValue(
				spinnerLinesOfCodeObserveSelectionObserveWidget,
				projectCharacteristicsLinesOfCodeObserveValue, null, null);
		//
		IObservableValue spinnerNumberOfComponentsObserveSelectionObserveWidget = SWTObservables
				.observeSelection(spinnerNumberOfComponents);
		IObservableValue projectCharacteristicsNumberOfComponentsObserveValue = PojoObservables
				.observeValue(projectCharacteristics, "numberOfComponents");
		bindingContext.bindValue(
				spinnerNumberOfComponentsObserveSelectionObserveWidget,
				projectCharacteristicsNumberOfComponentsObserveValue, null,
				null);
		//
		IObservableValue spinnerNumberOfTestcasesObserveSelectionObserveWidget = SWTObservables
				.observeSelection(spinnerNumberOfTestcases);
		IObservableValue projectCharacteristicsNumberOfTestCasesObserveValue = PojoObservables
				.observeValue(projectCharacteristics, "numberOfTestCases");
		bindingContext
				.bindValue(
						spinnerNumberOfTestcasesObserveSelectionObserveWidget,
						projectCharacteristicsNumberOfTestCasesObserveValue,
						null, null);
		//
		IObservableValue spinnerCodeCoverageObserveSelectionObserveWidget = SWTObservables
				.observeSelection(spinnerCodeCoverage);
		IObservableValue projectCharacteristicsPercentageOfCodeCoverageObserveValue = PojoObservables
				.observeValue(projectCharacteristics,
						"percentageOfCodeCoverage");
		bindingContext.bindValue(
				spinnerCodeCoverageObserveSelectionObserveWidget,
				projectCharacteristicsPercentageOfCodeCoverageObserveValue,
				null, null);
		//
		IObservableValue spinnerSuccessfulTestsObserveSelectionObserveWidget = SWTObservables
				.observeSelection(spinnerSuccessfulTests);
		IObservableValue projectCharacteristicsPercentageOfSuccessfulTestsObserveValue = PojoObservables
				.observeValue(projectCharacteristics,
						"percentageOfSuccessfulTests");
		bindingContext.bindValue(
				spinnerSuccessfulTestsObserveSelectionObserveWidget,
				projectCharacteristicsPercentageOfSuccessfulTestsObserveValue,
				null, null);
		//
		return bindingContext;
	}
}
